package com.five.calendar.ui.activity;

import android.app.Application;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.five.calendar.ApplicationHelper;
import com.five.calendar.R;
import com.five.calendar.alarmmanager.clock.AlarmManagerUtil;
import com.five.calendar.base.BaseActivity;
import com.five.calendar.common.observer.Observer;
import com.five.calendar.database.bean.ScheduleBean;
import com.five.calendar.database.observer.ScheduleObservable;
import com.five.calendar.ui.widget.ImportaceChooseLayout;
import com.five.calendar.ui.widget.TipsChooseLayout;
import com.five.calendar.ui.widget.picktime.DatePickDialogHelper;
import com.five.calendar.ui.widget.picktime.OnSureLisener;
import com.five.calendar.utils.TimeUtils;

import java.sql.Time;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xin on 2017/8/31.
 */

public class ScheduleAddActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.start_time)
    RelativeLayout mStartTimeLayout;
    @BindView(R.id.schedule_end_time)
    RelativeLayout mEndTimeLayout;
    @BindView(R.id.schedule_title)
    EditText mTitle;
    @BindView(R.id.schedule_note)
    EditText mNotes;
    @BindView(R.id.save_schedule)
    TextView mSaveSchedule;
    @BindView(R.id.start_time_show)
    TextView mStartTimeView;
    @BindView(R.id.schedule_end_time_show)
    TextView mEndTimeView;
    @BindView(R.id.schedule_address)
    EditText mAddressView;
    @BindView(R.id.schedule_importance)
    RelativeLayout mChooseImportaceItem;
    @BindView(R.id.importance_color)
    ImageView mImportanceIcon;
    @BindView(R.id.choose_importance_dialog)
    ImportaceChooseLayout mChooseImportanceDialog;
    @BindView(R.id.choose_tips_dialog)
    TipsChooseLayout mChooseTipsDialog;
    @BindView(R.id.ic_back)
    ImageView mBackIcon;
    @BindView(R.id.add_reminder)
    RelativeLayout mAddReminderItem;
    @BindView(R.id.reminder_text)
    TextView mReminderText;
    private Date mStartTime;
    private Date mEndTime;
    private long mStartTimeData;
    private long mEndTimeData;
    private int mImportance;
    private long mRemindTime;
    private int[] mImportances = new int[]{R.drawable.importance_icon_1, R.drawable.importance_icon_2, R.drawable.importance_icon_3, R.drawable.importance_icon_4};
    private boolean isCreate = true;
    private int isRemind;
    private ScheduleBean mScheduleBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule_add);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        ButterKnife.bind(this);
        initView();
        mStartTimeLayout.setOnClickListener(this);
        mEndTimeLayout.setOnClickListener(this);
        mSaveSchedule.setOnClickListener(this);
        mChooseImportaceItem.setOnClickListener(this);
        mAddReminderItem.setOnClickListener(this);
        mBackIcon.setOnClickListener(this);

        mChooseTipsDialog.setOnRadioChange(new TipsChooseLayout.OnRadioChange() {
            @Override
            public void onChanged(String str, long times) {
                mReminderText.setText(str);
                mRemindTime = times;
            }
        });

    }

    private void initView() {
        mChooseImportanceDialog.setCallBack(new ImportaceChooseLayout.CallBack() {
            @Override
            public void callBack(int importance) {
                mImportance = importance;
                mImportanceIcon.setImageResource(mImportances[importance]);
            }
        });
        if(getIntent().getExtras() != null){
            mScheduleBean = (ScheduleBean) getIntent().getExtras().getSerializable("schedulebean");
            mSaveSchedule.setText("Update");
            isCreate = false;
            mStartTime =  new Date(mScheduleBean.startTime);
            String startTime = TimeUtils.timeFormatString(mStartTime);
            mStartTimeView.setText(startTime);
            mStartTimeData = mStartTime.getTime();
            mEndTime = new Date(mScheduleBean.endTime);
            String endTime = TimeUtils.timeFormatString(mEndTime);
            mEndTimeView.setText(endTime);
            mEndTimeData = mEndTime.getTime();
            mTitle.setText(mScheduleBean.title);
            mAddressView.setText(mScheduleBean.address);
            mNotes.setText(mScheduleBean.note);
            mImportance = mScheduleBean.importance;
            mImportanceIcon.setImageResource(mImportances[mScheduleBean.importance]);
            mRemindTime = mScheduleBean.remindTime;
            int id = getIdByRemindTime(mScheduleBean.startTime, mRemindTime);
            RadioButton radioButton = (RadioButton) findViewById(id);
            mReminderText.setText(radioButton.getText());
            mChooseTipsDialog.checkRadioButton(id);
        }else{
            mStartTime = new Date();
            String startTime = TimeUtils.timeFormatString(mStartTime);
            mStartTimeView.setText(startTime);
            mStartTimeData = mStartTime.getTime();
            mEndTime = new Date();
            String endTime = TimeUtils.timeFormatString(mEndTime);
            mEndTimeView.setText(endTime);
            mEndTimeData = mEndTime.getTime();
        }
    }

    public static void startScheduleAddActivity(){
        Intent intent = new Intent(ApplicationHelper.getContext(), ScheduleAddActivity.class);
        ApplicationHelper.getContext().startActivity(intent);
    }

    public static void startScheduleAddActivity(ScheduleBean scheduleBean){
        Intent intent = new Intent(ApplicationHelper.getContext(), ScheduleAddActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("schedulebean", scheduleBean);
        intent.putExtras(bundle);
        ApplicationHelper.getContext().startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start_time:
                DatePickDialogHelper.showDatePickDialog(ScheduleAddActivity.this, new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        mStartTime = date;
                        String startTime = TimeUtils.timeFormatString(mStartTime);
                        mStartTimeView.setText(startTime);
                        mStartTimeData = mStartTime.getTime();
                    }
                });
                break;
            case R.id.schedule_end_time:
                DatePickDialogHelper.showDatePickDialog(ScheduleAddActivity.this, new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        mEndTime = date;
                        String endTime = TimeUtils.timeFormatString(mEndTime);
                        mEndTimeView.setText(endTime);
                        mEndTimeData = mEndTime.getTime();
                    }
                });
                break;
            case R.id.save_schedule:
                String title = mTitle.getText().toString();
                String address = mAddressView.getText().toString();
                String note = mNotes.getText().toString();
                int isRemind = mRemindTime == 0l ? 0 : 1;
                if(isCreate){
                    ScheduleObservable.saveSchedule(title, address, mStartTimeData, mEndTimeData, mImportance, note, isRemind, mRemindTime).registerObserver(new Observer<ScheduleBean, Integer, List<ScheduleBean>>() {
                        @Override
                        public void onFail(Integer integer) {

                        }

                        @Override
                        public void onComplete(List<ScheduleBean> scheduleBeans) {

                        }

                        @Override
                        public void onSuccess(ScheduleBean scheduleBean) {
                            Toast.makeText(ScheduleAddActivity.this,"save Successfully",Toast.LENGTH_SHORT).show();
                            if(scheduleBean.isRemind == 1){
                                AlarmManagerUtil.setAlarm(ScheduleAddActivity.this, 0, scheduleBean.startTime - scheduleBean.remindTime, (int)scheduleBean.id, 0, scheduleBean.title, 2);
                            }
                            finish();
                        }
                    }).startDataHandle();
                } else {
                    mScheduleBean.title = mTitle.getText().toString();
                    mScheduleBean.address = mAddressView.getText().toString();
                    mScheduleBean.note = mNotes.getText().toString();
                    mScheduleBean.remindTime = mRemindTime;
                    mScheduleBean.isRemind = mRemindTime == 0 ? 0 :1;
                    mScheduleBean.startTime = mStartTimeData;
                    mScheduleBean.endTime = mEndTimeData;
                    mScheduleBean.importance = mImportance;
                    ScheduleObservable.updateSchedule(mScheduleBean).registerObserver(new Observer<ScheduleBean, Integer, List<ScheduleBean>>() {
                        @Override
                        public void onFail(Integer integer) {

                        }

                        @Override
                        public void onComplete(List<ScheduleBean> scheduleBeen) {

                        }

                        @Override
                        public void onSuccess(ScheduleBean scheduleBean) {
                            if (scheduleBean.isRemind == 0){
                                AlarmManagerUtil.cancelAlarm(ScheduleAddActivity.this, AlarmManagerUtil.ALARM_ACTION, (int)scheduleBean.id);
                            } else {
                                AlarmManagerUtil.setAlarm(ScheduleAddActivity.this, 0, scheduleBean.startTime - scheduleBean.remindTime, (int)scheduleBean.id, 0, scheduleBean.title, 2);
                            }
                            Toast.makeText(ScheduleAddActivity.this, "update Successfully", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }).startDataHandle();
                }
                break;
            case R.id.schedule_importance:
                mChooseImportanceDialog.setVisibility(View.VISIBLE);
                break;
            case R.id.ic_back:
                finish();
                break;
            case R.id.add_reminder:
                mChooseTipsDialog.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if(mChooseTipsDialog.getVisibility() == View.VISIBLE){
            mChooseTipsDialog.setVisibility(View.GONE);
        } else if(mChooseImportanceDialog.getVisibility() == View.VISIBLE){
            mChooseImportanceDialog.setVisibility(View.GONE);
        } else {
            super.onBackPressed();
        }
    }

    public int getIdByRemindTime(long startTime, long mRemindTime){
        if (mRemindTime == TimeUtils.MINUTE * 10){
            return R.id.button1;
        } else if (mRemindTime == TimeUtils.MINUTE * 15){
            return R.id.button2;
        } else if (mRemindTime == TimeUtils.MINUTE * 30){
            return R.id.button3;
        } else if (mRemindTime == TimeUtils.MINUTE * 60){
            return R.id.button4;
        } else if (mRemindTime == TimeUtils.HOUR * 2){
            return R.id.button5;
        } else if (mRemindTime == TimeUtils.DAY) {
            return R.id.button6;
        } else if (mRemindTime == TimeUtils.DAY * 2){
            return R.id.button7;
        } else if (mRemindTime == TimeUtils.DAY * 7){
            return R.id.button8;
        }
        return R.id.button9;
    }
}
